package com.example.azheng.rxjavamvpdemo.ui.acitivity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.azheng.rxjavamvpdemo.R;
import com.example.azheng.rxjavamvpdemo.base.BaseMvpActivity;
import com.example.azheng.rxjavamvpdemo.bean.BaseObjectBean;
import com.example.azheng.rxjavamvpdemo.bean.login.LoginBean;
import com.example.azheng.rxjavamvpdemo.bean.userInfo.UserInfo;
import com.example.azheng.rxjavamvpdemo.mvp.contract.LoginContract;
import com.example.azheng.rxjavamvpdemo.mvp.presenter.LoginPresenter;
import com.example.azheng.rxjavamvpdemo.util.ProgressDialog;
import com.example.azheng.rxjavamvpdemo.util.UserInfoUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseMvpActivity<LoginPresenter> implements LoginContract.View {


    @BindView(R.id.etAccount)
    EditText etAccount;
    @BindView(R.id.view10)
    View view10;
    @BindView(R.id.etPassword)
    EditText etPassword;
    @BindView(R.id.view11)
    View view11;
    @BindView(R.id.checkBox)
    CheckBox checkBox;
    @BindView(R.id.tvForgetPassword)
    TextView tvForgetPassword;
    @BindView(R.id.tvRemenberPassword)
    TextView tvRemenberPassword;
    @BindView(R.id.tvLogin)
    TextView tvLogin;
    @BindView(R.id.tvRegist)
    TextView tvRegist;
    @BindView(R.id.tvCallServer)
    TextView tvCallServer;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        mPresenter = new LoginPresenter();
        mPresenter.attachView(this);
    }

    @Override
    public void showLoading() {
        ProgressDialog.getInstance().show(this);
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {
        Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLoginSuccess(BaseObjectBean<LoginBean> bean) {

        if (null == bean) {
            return;
        }

        String msg = bean.getMsg();

        if (TextUtils.isEmpty(msg)) {
            Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
        }

        int code = bean.getCode();
        String token = bean.getData().getToken();
        Log.i("MyTest", "token:" + token);
        if (0 == code) {
            UserInfoUtils.setLogged();
            UserInfoUtils.setToken(token);
            mPresenter.userInfo();
            finish();
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }

    }

    @Override
    public void onUserInfoSuccess(BaseObjectBean<UserInfo> bean) {

        if (null == bean) {
            return;
        }

        UserInfoUtils.setUserInfo(bean);

    }

    @OnClick({R.id.tvLogin, R.id.tvRegist, R.id.tvCallServer, R.id.tvForgetPassword})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvLogin:
                String account = etAccount.getText().toString();
                String password = etPassword.getText().toString();
                if (TextUtils.isEmpty(account)) {
                    Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                mPresenter.login(account, password);
                break;
            case R.id.tvRegist:
                //跳转注册
                doActivity(RegistActivity.class);
                break;
            case R.id.tvCallServer:
                break;
            case R.id.tvForgetPassword:
                Intent intent = new Intent(this, ForgetPasswordActivity.class);
                startActivity(intent);
                break;
        }
    }
}
